package com.hxkj.thread;

import com.hxkj.domain.po.*;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Component
public class ThreadPoolManager {

    //用于从IOC里取对象
    private BeanFactory factory; //如果实现Runnable的类是通过spring的application.xml文件进行注入,可通过 factory.getBean()获取，这里只是提一下

    // 线程池维护线程的最少数量
    private final static int CORE_POOL_SIZE = 2;
    // 线程池维护线程的最大数量
    private final static int MAX_POOL_SIZE = 100;
    // 线程池维护线程所允许的空闲时间
    private final static int KEEP_ALIVE_TIME = 0;
    // 线程池所使用的缓冲队列大小
    private final static int WORK_QUEUE_SIZE = 10000;

    private RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();

    //private ThreadPoolExecutor threadPoolExecutor;

    final ThreadPoolExecutor pool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS, new ArrayBlockingQueue(WORK_QUEUE_SIZE), this.handler);

    public void genArticleFiles(List<Article> list){
        GenFileJob<Article> job = new GenFileJob<>(list);
        pool.execute(job);
    }

    public void genChannelFiles(List<Channel> list){
        GenFileJob<Channel> job = new GenFileJob<>(list);
        pool.execute(job);
    }

    public void genColumnFiles(List<Column> list){
        GenFileJob<Column> job = new GenFileJob<>(list);
        pool.execute(job);
    }

    public void genTagInfoFiles(List<Tags> list){
        GenFileJob<Tags> job = new GenFileJob<>(list);
    }

    public void genTagsFiles(List<SiteUniversal> list){
        GenFileJob<SiteUniversal> job = new GenFileJob<>(list);
        pool.execute(job);
    }

}
